<?php



	/**
	 * 粑粑的权限（分销）
	 */
	function get_pow_f($id){
		$res=M('user')->where('u_id='.$id)->find();
		if($res){
		return $res['ck_fenxiao'];
		} 
		return 0;
	}
	/**
	 * 渠道id换取渠道昵称
	 */
	function re_qudao_nick_name($id){
		$res=M('qudao')->where('qudao_id='.$id)->getField('qudao_nick_name');
		return $res;
		
	}
	/**
	 * id转换地址
	 */
	function re_region_address($id){
		$res=M('region')->where('ID='.$id)->getField('REGION_NAME');
		return $res;
	}
	/**
	 * 按揭员商品id扎u你很好小明转换小名
	 */
	function re_letter_name($id){
		$res=M('goods')->where('id='.$id)->getField('litter_name');
		return $res;
	}
	/**
	 * 渠道转换
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	function re_data_web($id){
		if($id==0){
			return '全区';
		}
		$res=M('region')->where("id=$id")->find();
		return $res['region_name'];
	}
	/**
	 * 商品id获取商品最低额度
	 */
	function get_min_ed($g_id){
	      $where['g_id']=$g_id;
	      $temp=M('lx')->where($where)->select();
	      $my_temp['min_ed']=$temp[0]['min_ed'];
	      foreach ($temp as $k => $v) {
	         if($temp[$key]['min_ed']<$my_temp['min_ed']){
	            $my_temp['min_ed']=$temp[$k]['min_ed'];
	         }
	      }
	      return  $my_temp['min_ed'];
	}
	/**
	 * 商品id获取商品最高额度
	 */
	function get_max_ed($g_id){
	      $where['g_id']=$g_id;
	      $temp=M('lx')->where($where)->select();
	      $my_temp['max_ed']=$temp[0]['max_ed'];
	      foreach ($temp as $k => $v) {
	         if($temp[$key]['max_ed']>$my_temp['max_ed']){
	            $my_temp['max_ed']=$temp[$k]['max_ed'];
	         }
	      }
	      return  $my_temp['max_ed'];
	}
	/**
	 * 商品id获取商品最低利息
	 */
	function get_min_lx($g_id){
	      $where['g_id']=$g_id;
	      $temp=M('lx')->where($where)->select();
	      $my_temp['min_lx']=$temp[0]['min_lx'];
	      foreach ($temp as $k => $v) {
	         if($temp[$key]['min_lx']<$my_temp['min_lx']){
	            $my_temp['min_lx']=$temp[$k]['min_lx'];
	            $my_temp['repayments']=$temp[$k]['repayments']; 
	         }
	      }
	      return  $my_temp['min_lx'];
	}
	/**
	 * 商品id获取商品最高利息
	 */
	function get_max_lx($g_id){
	      $where['g_id']=$g_id;
	      $temp=M('lx')->where($where)->select();
	      $my_temp['max_lx']=$temp[0]['max_lx'];
	      foreach ($temp as $k => $v) {
	         if($temp[$key]['max_lx']>$my_temp['max_lx']){
	            $my_temp['max_lx']=$temp[$k]['max_lx'];
	         }
	      }
	      return  $my_temp['max_lx'];
	}
	/**
	 * 获取web用户子账号名称
	 */
	function get_web_f_mesage($web_id){
	   $p_u_id=$web_id;
	   $u_name=M('user')->where('u_id='.$p_u_id)->getField('u_name');
	   return $u_name;
	}
	/**
	 * 获取咨询个数
	 * @return [type] [description]
	 */
   function get_web_chat_count($web_id){
   	  $res=M('chat')->where('second_user='.$web_id)->count();
   	  return $res;
   }
   /**
    * 一级咨询人数
    */
   function get_web_frist_count($web_id){
   	  $res=M('chat')
   	  	   ->alias('a')
   	  	   ->join('bm_web_user b ON a.second_user=b.web_id','LEFT')
   	  	   ->where('b.p_id='.$web_id)
   	  	   ->count();
   	  return $res;
   }
   /**
    * 二级咨询
    */
   function get_web_second_count($web_id){
   		$res=M('web_user')
   			 ->where('p_id='.$web_id)
   			 ->select();
   		foreach ($res as $key => $value) {
   			
   			$arr[$key]=M('chat')
			   	  	   ->alias('a')
			   	  	   ->join('bm_web_user b ON a.second_user=b.web_id','LEFT')
			   	  	   ->where('b.p_id='.$value['web_id'])
			   	  	   ->count();
   		}
   		$arr=array_sum($arr);
   		if(empty($arr))return 0;
   	    return $arr;
   }
   /**
    * main最新咨询
    */
   function get_new_select($web_id){
   	  $res0=M('chat')->where('second_user='.$web_id)->count();
   	  $web_id_0=$web_id;
	  $res1=M('chat')
	  	   ->alias('a')
	  	   ->join('bm_web_user b ON a.second_user=b.web_id','LEFT')
	  	   ->where('b.p_id='.$web_id)
	  	   ->count();
	  $sel_res1=M('chat')
	  	   ->alias('a')
	  	   ->join('bm_web_user b ON a.second_user=b.web_id','LEFT')
	  	   ->where('b.p_id='.$web_id)
	  	   ->select();
	  foreach ($sel_res1 as $k_sr1 => $v_sr1) {
	  	$web_id_1.=$v_sr1['second_user'];
	    if($k_sr1<count($sel_res1)-1){
	          $web_id_1.=',';
	    }
	  }
	  $res2=M('web_user')
			 ->where('p_id='.$web_id)
			 ->select();
	  foreach ($res2 as $key => $value) {
				$arr[$key]=M('chat')
		   	  	   ->alias('a')
		   	  	   ->join('bm_web_user b ON a.second_user=b.web_id','LEFT')
		   	  	   ->where('b.p_id='.$value['web_id'])
		   	  	   ->count();
		   	  	$sel_res2[$key]=M('chat')
		   	  	   ->alias('a')
		   	  	   ->join('bm_web_user b ON a.second_user=b.web_id','LEFT')
		   	  	   ->where('b.p_id='.$value['web_id'])
		   	  	   ->select();
				foreach ($sel_res2[$key] as $k_sr2 => $v_sr2) {
				  	$web_id_2.=$v_sr2['second_user'];
				    if($k_sr2<count($sel_res2[$key])-1){
				          $web_id_2.=',';
				    }
				}
	  }
	  $arr=array_sum($arr);
	  if(empty($arr))$arr=0;
	  
	  	//$where0['apply_data']=array('neq',null);
   		$wh0['b.web_id']=array('in',$web_id_0);
   		$wh0['_logic']='or';
   		$where0['_complex']=$wh0;
   		$where0['a.chat_id']=array('gt','0');
   		$where0['a.c_id']=array('gt','0');
   		$where0['a.data_id']=array('gt','0');
   		//$where0['a.cd_id']=array('gt','0');
   		$res[0]=M('lccdc')
   			 ->alias('a')
   			 ->join('bm_web_user b ON a.web_id=b.web_id','LEFT')
   			 ->join('bm_shenqing c ON a.cd_id=c.cd_id','LEFT')
   			 ->join('bm_web_user_data d ON a.c_id=d.c_id','LEFT')
   			 ->join('bm_search_data e ON a.data_id=e.data_id','LEFT')
   			 ->where($where0)
   			 ->group('a.lccdc_id')
   			 ->order('lccdc_id desc')
   			 ->find();
   		$res[0]['count_zx']=$res0;
   		//$where1['apply_data']=array('neq',null);
   		//$wh['b.web_link_data']=array('LIKE',"%_".$web_id."_%");
   		$wh1['b.web_id']=array('in',$web_id_1);
   		$wh1['_logic']='or';
   		$where1['_complex']=$wh1;
   		$where1['a.chat_id']=array('gt','0');
   		$where1['a.c_id']=array('gt','0');
   		$where1['a.data_id']=array('gt','0');
   		//$where1['a.cd_id']=array('gt','0');
   		if($web_id_1){
	   		$res[1]=M('lccdc')
	   			 ->alias('a')
	   			 ->join('bm_web_user b ON a.web_id=b.web_id','LEFT')
	   			 ->join('bm_shenqing c ON a.cd_id=c.cd_id','LEFT')
	   			 ->join('bm_web_user_data d ON a.c_id=d.c_id','LEFT')
	   			 ->join('bm_search_data e ON a.data_id=e.data_id','LEFT')
	   			 ->where($where1)
	   			 ->group('a.lccdc_id')
	   			 ->order('lccdc_id desc')
	   			 ->find();

   		}
   		$res[1]['count_zx']=$res1;

   		//$where2['apply_data']=array('neq',null);
   		//$wh['b.web_link_data']=array('LIKE',"%_".$web_id."_%");
   		$wh2['b.web_id']=array('in',$web_id_2);
   		$wh2['_logic']='or';
   		$where2['_complex']=$wh2;
   		$where2['a.chat_id']=array('gt','0');
   		$where2['a.c_id']=array('gt','0');
   		$where2['a.data_id']=array('gt','0');
   		//$where2['a.cd_id']=array('gt','0');
   		if($web_id_2){
	   		$res[2]=M('lccdc')
	   			 ->alias('a')
	   			 ->join('bm_web_user b ON a.web_id=b.web_id','LEFT')
	   			 ->join('bm_shenqing c ON a.cd_id=c.cd_id','LEFT')
	   			 ->join('bm_web_user_data d ON a.c_id=d.c_id','LEFT')
	   			 ->join('bm_search_data e ON a.data_id=e.data_id','LEFT')
	   			 ->where($where2)
	   			 ->group('a.lccdc_id')
	   			 ->order('lccdc_id desc')
	   			 ->find();
   		}
   		$res[2]['count_zx']=$arr;
   		return $res;

   }
   /**
    * 获取子账号login
    */
   function get_user_f_logo($web_id){
   		if(!$web_id)return 0;
	   	$res =	M('web_user')
	   		->alias('a')
	   		->join('bm_user b ON a.p_u_id=b.u_id')
	   		->where('a.web_id='.$web_id)
	   		->getField('login_img');
   		return $res;
   }
   /**
    * 渲染商品页面辅助推荐
    * @DateTime:    [2018-03-09 16:30:56]
    */
   function get_ajtj($goods_id){
   		$where['tj_g_id']=$goods_id;
   		$where['tj_web_id']=session('user.web_id');//查询时候点击按揭员生成
   		$res=M('ajtj')->where($where)->find();
   		if($res){
   			$where1['web_id']=session('user.web_id');
   			$ro=M('web_user')->where($where1)->find();
   			return $ro['web_name'].'推荐';
   		}
   		else{
   			return 0;
   		}

   }
   function get_nave_ajtj($goods_id,$web_id){
   		$where['tj_g_id']=$goods_id;
   		$where['tj_web_id']=$web_id;
   		$res=M('ajtj')->where($where)->find();
   		if($res){
   			$where1['web_id']=$web_id;
   			$ro=M('web_user')->where($where1)->find();
   			return $ro['web_name'].'推荐';
   		}
   		else{
   			return 0;
   		}
   }
   /**
    * web_id获取用户昵称
    */
   function get_web_nickname($to_user){
   		$where['to_user_group_id']=$to_user;
   		$res=M('web_user')->alias('a')->join('bm_to_user_group b ON a.web_id=b.to_user_group')->where($where)->getField('web_name');
   		return $res;
   }
	/**
	* web_id按揭员
	*/
   function get_web_nickname_f($from_user){
   		$where['from_user_group_id']=$from_user;
   		$from_user_group=M('from_user_group')->where($where)->getField('from_user_group');
   		$arr_from_group=explode(',',$from_user_group);
   		$where_from['web_id']=$arr_from_group[0];
   		$res=M('web_user')->where($where_from)->getField('web_name');
   		return $res;
   }
	/**
	* web_id业务员
	*/
   function get_web_nickname_f_ole($from_user){
   		$where['from_user_group_id']=$from_user;
   		$from_user_group=M('from_user_group')->where($where)->getField('from_user_group');
   		$arr_from_group=explode(',',$from_user_group);
   		$where_from['web_id']=$arr_from_group[1];
   		$res=M('web_user')->where($where_from)->getField('web_name');
   		return $res;
   }
	/**
	 * 标签id转换名称
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	function biaoqian_name_web($id){
		$where['id']=$id;
		$res=M('biaoqian')->where($where)->getField('name');
		return $res;
	}
   /**
    * 标签获取最低利息
    * @param  [type] $bq_id [description]
    * @return [type]        [description]
    */
	function bq_get_min_lx($bq_id){
		  $wherebq['cp_temp']=array('LIKE','%'.$bq.'%');
		  $goods_all=M('goods')->where($whereb)->field('id')->select();
		  foreach ($goods_all as $key => $value) {
		  	  $g_id[$key]=$goods_all[$key]['id'];
		  }
	      $where['g_id']=array('IN',implode(',',$g_id));
	      $temp=M('lx')->where($where)->select();
	      $my_temp['min_lx']=$temp[0]['min_lx'];
	      foreach ($temp as $k => $v) {
	         if($temp[$key]['min_lx']<$my_temp['min_lx']){
	            $my_temp['min_lx']=$temp[$k]['min_lx'];
	         }
	      }
	      return  $my_temp['min_lx'];
	}
	/**
	 * 商品id判断置换自定义名称
	 * @DateTime:    [2018-03-15 12:13:53]
	 * @return [type] [description]
	 */
	function fun_nick_name($goods_id){
		if(empty($goods_id))return false;
		$goods_name_state=M('goods_ad')->where("g_id=".$goods_id)->getField('goods_name_state');
		if($goods_name_state==1){
			$web_id=session('user.web_id');
			$p_u_id=M('web_user')->where("web_id={$web_id}")->getField('p_u_id');
			$where['u_id']=$p_u_id;
			$where['g_id']=$goods_id;
			$nick_name=M('nick_name')->where($where)->getField('nick_name');
			if($nick_name){
				return $nick_name;
			}
			else{
				return false;
			}

		}
		else{
			return false;
		}

	}
	/**
	 * 单独处理web_id转换用户昵称
	 */
	function get_web_nick_name($web_id){
		if(!$web_id)return "渠";
		$where['web_id']=$web_id;
		$nick_name=M('web_user')->where($where)->getField('web_name');
		return $nick_name;
	}
	/**
	 * 聊天室chat_id
	 * 按揭员手机
	 */
	function chat_aj_data($chat_id){
		if(!$chat_id)return '';

		$where['chat_id']=$chat_id;
		$chat_data=M('chat')->where($where)->find();
		$phone=M('web_user')->where('web_id='.$chat_data['first_user'])->getField('web_phone');
		return $phone;
	}
	/**
	 * 获取了渠道聊天平均时间
	 */
	function re_avg_time($web_id){
		if(!$web_id)return '';
		$web_user=M('web_user')->where('web_id='.$web_id)->find();
		$star_time=strtotime($web_user['web_time']);
		$avg=($web_user['re_time']/$web_user['re_sec'])-$web_user['start_time'];
		if(empty($web_user['re_time'])){
			$avg_time='∞';
		}
		else{
	        $avg_time = '00:00:00';  
	        if ($avg>0) { 

	                $hour = floor($avg/3600);  
	                $minute = floor(($avg-3600 * $hour)/60);  
	                $second = floor((($avg-3600 * $hour) - 60 * $minute) % 60);  
	                $avg_time = $hour.':'.$minute.':'.$second;  
	        }  			
		}
		return $avg_time;
	}